import { getClient } from '@tauri-apps/api/http';
import type { RequestOptions,Body,HttpOptions,Response } from '@tauri-apps/api/http';
import { message } from '@/utils/message';
import { setCookie } from '@/utils/cookie';

const client = await getClient({
  connectTimeout: 10
});
const requestInterceptor=():RequestOptions =>{
  const opts:any={}
  opts.headers={
    cookie: document.cookie
  }
  return opts;
}
const responseInterceptor=<T>(_options:RequestOptions,response:Response<T>):Promise<T> =>{
  return new Promise((resove,reject) =>{
    const cookies=response.rawHeaders['set-cookie'];
    if(cookies){
      for(let c of cookies){
        const reg=new RegExp(`^(.*?)=(.*?) *; *expires=(.*?) *;`);
        const arr=c.match(reg);
        if(arr) setCookie(arr[1],arr[2],(new Date(arr[3])).getTime());
      }
    }
    if(response.ok){
      resove(response.data);
    }else{
      message('error','请求错误');
      reject()
    }
  })
}
const errorInterceptor=(err:any) =>{
  console.error(err);
  message('error','网络错误');
  return Promise.reject(err);
}
export default {
  delete<T>(url:string,options?:RequestOptions):Promise<T>{
    const _opts=Object.assign(requestInterceptor(),options);
    return client.delete(url,_opts).then((response:Response<any>) =>{
      return responseInterceptor<T>(_opts,response);
    }).catch(err =>{
      return errorInterceptor(err);
    });
  },
  drop():Promise<void>{
    return client.drop().catch(err =>{
      return errorInterceptor(err);
    });
  },
  get<T>(url:string,options?:RequestOptions):Promise<T>{
    const _opts=Object.assign(requestInterceptor(),options);
    return client.get(url,_opts).then((response:Response<any>) =>{
      return responseInterceptor<T>(_opts,response);
    }).catch(err =>{
      return errorInterceptor(err);
    });
  },
  patch<T>(url:string,options?:RequestOptions):Promise<T>{
    const _opts=Object.assign(requestInterceptor(),options);
    return client.patch(url,_opts).then((response:Response<any>) =>{
      return responseInterceptor<T>(_opts,response);
    }).catch(err =>{
      return errorInterceptor(err);
    });
  },
  post<T>(url:string,body:Body,options?:RequestOptions):Promise<T>{
    const _opts=Object.assign(requestInterceptor(),options);
    return client.post(url,body,_opts).then((response:Response<any>) =>{
      return responseInterceptor<T>(_opts,response);
    }).catch(err =>{
      return errorInterceptor(err);
    });
  },
  put<T>(url:string,body:Body,options?:RequestOptions):Promise<T>{
    const _opts=Object.assign(requestInterceptor(),options);
    return client.put(url,body,_opts).then((response:Response<any>) =>{
      return responseInterceptor<T>(_opts,response);
    }).catch(err =>{
      return errorInterceptor(err);
    });
  },
  request<T>(options: HttpOptions):Promise<T>{
    const _opts=Object.assign(requestInterceptor(),options);
    return client.request(_opts).then((response:Response<any>) =>{
      return responseInterceptor<T>(_opts,response);
    }).catch(err =>{
      return errorInterceptor(err);
    });
  }
};